package com.zhangzhan.gulimallmember.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallmember.dao.MemberReceiveAddressDao;
import com.zhangzhan.gulimallmember.entity.MemberReceiveAddressEntity;
import com.zhangzhan.gulimallmember.service.MemberReceiveAddressService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<MemberReceiveAddressEntity> getMemberAddrList(Long memberId) {
        return  this.list(Wrappers.<MemberReceiveAddressEntity>lambdaQuery()
                .eq(MemberReceiveAddressEntity::getMemberId,memberId));
    }

    @Override
    public MemberReceiveAddressEntity getFreight(Long addrId) {
        MemberReceiveAddressEntity address = this.getById(addrId);
        String freight = address.getPhone().substring(address.getPhone().length() - 1);
        address.setFreight(new BigDecimal(freight));
        return address;
    }

}